package org.halk.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.halk.entity.User;
import org.halk.mapper.TestMapper;
import org.halk.service.TestService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TestServiceImpl implements TestService {

    private final TestMapper testMapper;


    @Override
    public void insertList() {
        ArrayList<User> userList = new ArrayList<>();


        for (int i = 0; i < 100; i++) {
            User user = new User()
                    .setAge(i + 1)
//                    .setCreateTime(new Timestamp(System.currentTimeMillis() + i * 10))
                    .setCreateTime(new Date())
                    .setName("第" + i + "个");
            System.out.println(user);
            userList.add(user);
        }

        System.out.println("===============");

        userList.forEach(this.testMapper::insert);


    }

    @Override
    public List<User> getAll() {
        List<User> selectList = this.testMapper.selectList(Wrappers.lambdaQuery(User.class));
        selectList.sort((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()));
        selectList.forEach(System.out::println);

        return selectList;
    }

    @Override
    public List<User> findUser() {
        return this.testMapper.findUser();
    }

    @Override
    @Transactional()
    public void test01() {
        test02();
    }


    public void test02() {

        test03();
        test04();

        User user = new User()
                .setAge(88774)
                .setCreateTime(new Date())
                .setName("第" + 123 + "个");
        System.out.println(user);
        System.out.println("===============");
        this.testMapper.insert(user);

        int i = 1 / 0;
        System.out.println(1111);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void test04() {

        User user = new User()
                .setAge(88773)
                .setCreateTime(new Date())
                .setName("第" + 123 + "个");
        System.out.println(user);
        System.out.println("===============");
        this.testMapper.insert(user);

//        int i = 1/0;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void test03() {

        User user = new User()
                .setAge(88772)
                .setCreateTime(new Date())
                .setName("第" + 123 + "个");
        System.out.println(user);
        System.out.println("===============");
        this.testMapper.insert(user);
    }
}
